/**
 * @author zhangxiaofei [<1326336909@qq.com>]
 * @dateTime 2023-02-08
 *
 * 全局公共函数封装
 */

import { ref, reactive} from 'vue'
import { useRouter } from "vue-router"
import { ElMessage as Message } from 'element-plus'

export function base() {

  const dataList = reactive([])
  const pageIndex = ref(1)
  const pageSize = ref(10)
  const totalPage = ref(0)
  const dataListLoading = ref(false)
  const dataListSelections = reactive([])


  /**
   * @author zhangxiaofei [<1326336909@qq.com>]
   * @dateTime 2023-02-10
   *
   * 设置每页显示条数
   *
   * @param {[type]} size 显示条数
   * @return {[type]}
   */
  const sizeChangeHandle = (size) => {
    this.pageSize = size
    this.pageIndex = 1
    this.getDataList()
  }


  /**
   * @author zhangxiaofei [<1326336909@qq.com>]
   * @dateTime 2023-02-10
   *
   * 设置当前页数
   *
   * @param {[type]} size 当前页数
   * @return {[type]}
   */
  const currentChangeHandle = (size) => {
    this.pageIndex = size
    this.getDataList()
  }

  /**
   * @author zhangxiaofei [<1326336909@qq.com>]
   * @dateTime 2023-02-10
   *
   * 多选函数
   *
   * @param {[type]} val 行号
   * @return {[type]}
   */
  const selectionChangeHandle = (val) => {
    this.dataListSelections = val
  }

  /**
   * @author zhangxiaofei [<1326336909@qq.com>]
   * @dateTime 2023-02-10
   *
   * 获取数据列表
   *
   * @param {[type]} is_search 是否为搜索操作
   * @return {[type]}
   */
  const getDataList = (is_search) => {
    is_search = is_search || false

    if (is_search) {
      this.pageIndex = 1
    }

    this.dataListLoading = true

    $http({
      url: $http.adornUrl('/' + this.model + '/list'),
      method: 'get',
      params: $http.adornParams({
        is_search: is_search,
        page: this.pageIndex,
        limit: this.pageSize,
        name: this.dataForm.name,
        head: this.dataForm.head,
        title: this.dataForm.title,
        mobile: this.dataForm.mobile,
        username: this.dataForm.username,
        nickname: this.dataForm.nickname,
        realname: this.dataForm.realname,
        id_card_no: this.dataForm.id_card_no,
        content: this.dataForm.content,
        category: this.dataForm.category,
        pid: this.dataForm.pid,
        role_id: this.dataForm.role_id,
        unit_id: this.dataForm.unit_id,
        parent_id: this.dataForm.parent_id,
        category_id: this.dataForm.category_id,
        industry_id: this.dataForm.industry_id,
        add_time: this.dataForm.add_time,
        valid_time: this.dataForm.valid_time,
        create_time: this.dataForm.create_time,
        organization_id: this.dataForm.organization_id,
        audit_status: this.dataForm.audit_status,
        order_status: this.dataForm.order_status,
        certification_status: this.dataForm.certification_status,
        is_hidden: this.dataForm.is_hidden,

        archive_contact: this.dataForm.contact,
        archive_contact_mobile: this.dataForm.contact_mobile,

        member_username: this.dataForm.member_username,
        member_nickname: this.dataForm.member_nickname,

        customer_title: this.dataForm.customer_title,
      }),
    }).then(({ data }) => {
      if (data && data.status === 200) {
        this.dataList = data.data.data
        this.totalPage = data.data.total
      } else {
        this.dataList = []
        this.totalPage = 0
        this.$message.error(data.message)
      }
      this.dataListLoading = false
    })
  }

  // 改变状态
  const handleStatus = ($event, id, field) => {
    this.$http({
      url: this.$http.adornUrl('/' + this.model + '/status'),
      method: 'post',
      data: {
        id: id,
        field: field,
        value: $event,
      },
    }).then(({ data }) => {
      if (data && data.status === 200) {
        this.$message({
          message: this.$t('common.handle_success'),
          type: 'success',
          duration: 1500,
          onClose: () => {
            this.getDataList()
          },
        })
      } else {
        this.$message.error(this.$t(data.message))
      }
    })
  }

  // 删除
  const deleteHandle = (id) => {
    var ids = id ? [id] : this.dataListSelections.map((item) => {return item.id})

    let message = this.$t('common.to_determine_the') +
                  this.$t('common.current_check') +
                  this.$t('common.for') +
                  `${id ? this.$t('common.delete') : this.$t('common.batch_delete')}` +
                  this.$t('common.handle') + `?`

    if (this.message != undefined && this.message.error != undefined) {
      message = this.message.error
    }

    this.$confirm(message, this.$t('common.prompt'), {
      confirmButtonText: this.$t('common.confirm'),
      cancelButtonText: this.$t('common.cancel'),
      type: 'warning',
    })
      .then(() => {
        this.$http({
          url: this.$http.adornUrl('/' + this.model + '/delete'),
          method: 'post',
          data: { id: this.$http.adornData(ids, false) },
        }).then(({ data }) => {
          if (data && data.status === 200) {
            this.$message({
              message: this.$t('common.handle_success'),
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              },
            })
          } else {
            this.$message.error(this.$t(data.message))
          }
        })
      })
      .catch(() => {})
  }





  return {
    dataList,
    pageIndex,
    pageSize,
    totalPage,
    dataListLoading,
    dataListSelections,
    sizeChangeHandle,
    currentChangeHandle,
    selectionChangeHandle,
    getDataList,
    handleStatus,
    deleteHandle,
  }
}
